package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;

import com.atguigu.lease.web.admin.vo.fee.FeeValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.ibatis.javassist.expr.NewArray;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * projectName: com.atguigu.lease.web.admin.service.impl
 *
 * @author: Aiolin
 * description:
 * time: 2025/5/7 上午9:58
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>implements ApartmentInfoService {

    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private ApartmentFacilityService apartmentFacilityService;
    @Autowired
    private ApartmentFeeValueService apartmentFeeValueService;
    @Autowired
    private ApartmentLabelService apartmentLabelService;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private ApartmentFacilityMapper apartmentFacilityMapper;
    @Autowired
    private ApartmentLabelMapper apartmentLabelMapper;
    @Autowired
    private ApartmentFeeValueMapper apartmentFeeValueMapper;
    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private FeeValueMapper feeValueMapper;
    @Autowired
    private CityInfoService cityInfoService;
    @Autowired
    private ProvinceInfoService provinceInfoService;
    @Autowired
    private DistrictInfoService districtInfoService;
    @Autowired
    ApartmentInfoMapper apartmentInfoMapper;

    @Override
    public void saveOrUpdateApartment(ApartmentSubmitVo apartmentSubmitVo) {
        Long id = apartmentSubmitVo.getId();
        if (id != null) {
            //修改
            //删除公寓和图片之间的关系
            LambdaQueryWrapper<GraphInfo> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(GraphInfo::getItemId, id);
            queryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT);
            graphInfoService.remove(queryWrapper);
            //删除公寓和配套i之间的关系
            LambdaQueryWrapper<ApartmentFacility> facilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
            facilityLambdaQueryWrapper.eq(ApartmentFacility::getFacilityId, id);
            apartmentFacilityService.remove(facilityLambdaQueryWrapper);
            //删除公寓和杂费之间的关系
            LambdaQueryWrapper<ApartmentFeeValue> feeValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
            feeValueLambdaQueryWrapper.eq(ApartmentFeeValue::getFeeValueId, id);
            apartmentFeeValueService.remove(feeValueLambdaQueryWrapper);
            //删除公寓和标签之间的关系
            LambdaQueryWrapper<ApartmentLabel> labelLambdaQueryWrapper = new LambdaQueryWrapper<>();
            labelLambdaQueryWrapper.eq(ApartmentLabel::getLabelId, id);
            apartmentLabelService.remove(labelLambdaQueryWrapper);
        }
        //添加，修改
        //表单只提交了省市区的id，没有提交名称，业务层自己处理
        apartmentSubmitVo.setProvinceName(provinceInfoService.getById(apartmentSubmitVo.getProvinceId()).getName());
        apartmentSubmitVo.setCityName(cityInfoService.getById(apartmentSubmitVo.getCityId()).getName());
        apartmentSubmitVo.setDistrictName(districtInfoService.getById(apartmentSubmitVo.getDistrictId()).getName());

        //1.保存公寓数据
        super.saveOrUpdate(apartmentSubmitVo);
        id = apartmentSubmitVo.getId();
        //主键回填
        //2.保存图片数据
        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
        //将数据放入集合
        if (!CollectionUtils.isEmpty(graphVoList)) {
            List<GraphInfo> graphInfoList = new ArrayList<>();
            for (GraphVo graphVo : graphVoList) {
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setName(graphVo.getName());
                graphInfo.setUrl(graphVo.getUrl());
                graphInfo.setItemType(ItemType.APARTMENT);
                graphInfo.setItemId(id);
                graphInfoList.add(graphInfo);
            }
            graphInfoService.saveBatch(graphInfoList);//批量保存
        }
        //3.保存配套列表
        List<Long> facilityInfoIdList = apartmentSubmitVo.getFacilityInfoIds();
        if (!CollectionUtils.isEmpty(facilityInfoIdList)) {
            List<ApartmentFacility> facilityList = new ArrayList<>();
            for (Long facilityInfoId : facilityInfoIdList) {
                /**apartmentFacility = new ApartmentFacility();//@builder不生成无参构造，手动添加无参构造或者注释@builder
                 apartmentFacility.setApartmentId(id);
                 apartmentFacility.setFacilityId(facilityId);*/
                ApartmentFacility apartmentFacility = ApartmentFacility.builder()
                        .apartmentId(id)
                        .facilityId(facilityInfoId)
                        .build();
                facilityList.add(apartmentFacility);

            }
        }
        //4.保存公寓标签
        List<Long> labelIdList = apartmentSubmitVo.getLabelIds();
        if (!CollectionUtils.isEmpty(labelIdList)) {
            List<ApartmentLabel> labelList = new ArrayList<>();
            for (Long labelId : labelIdList) {
                ApartmentLabel apartmentLabel = ApartmentLabel.builder().
                        apartmentId(id).labelId(labelId).build();
                labelList.add(apartmentLabel);
            }
            apartmentLabelService.saveBatch(labelList);
        }
        //5.保存公寓杂费
        List<Long> feeValueIdList = apartmentSubmitVo.getFeeValueIds();
        if (!CollectionUtils.isEmpty(feeValueIdList)) {
            List<ApartmentFeeValue> feeKeyList = new ArrayList<>();
            for (Long feeValueId : feeValueIdList) {
                ApartmentFeeValue apartmentFeeValue = ApartmentFeeValue.builder()
                        .apartmentId(id).feeValueId(feeValueId).build();
                feeKeyList.add(apartmentFeeValue);
            }
            apartmentFeeValueService.saveBatch(feeKeyList);
        }

    }

    @Override
    public void removeApartmentById(Long id) {
        LambdaQueryWrapper<RoomInfo> roomInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomInfoLambdaQueryWrapper.eq(RoomInfo::getApartmentId, id);
        //0.删除公寓，判断是否存在房间
        Long count = roomInfoMapper.selectCount(new LambdaQueryWrapper<RoomInfo>().eq(RoomInfo::getApartmentId, id));
        if (count > 0) {
            throw new LeaseException(ResultCodeEnum.DELETE_ERROR);
        }
        //1.删除主表数据
        super.removeById(id);
        //2.删除公寓关联图片数据
        graphInfoMapper.delete(new LambdaQueryWrapper<GraphInfo>()
                .eq(GraphInfo::getItemId, id)
                .eq(GraphInfo::getItemType, ItemType.APARTMENT));
        //3.删除公寓和配套关联数据
        apartmentFacilityMapper.delete(new LambdaQueryWrapper<ApartmentFacility>().eq(ApartmentFacility::getFacilityId, id));
        //4.删除公寓与标签关联数据
        apartmentLabelMapper.delete(new LambdaQueryWrapper<ApartmentLabel>().eq(ApartmentLabel::getLabelId, id));
        //5.删除公寓和杂费关联数据
        apartmentFeeValueMapper.delete(new LambdaQueryWrapper<ApartmentFeeValue>().eq(ApartmentFeeValue::getApartmentId, id));
    }

    @Override
    public IPage<ApartmentItemVo> pageQuery(IPage<ApartmentItemVo> page, ApartmentQueryVo queryVo) {
        return apartmentInfoMapper.pageQuery(page, queryVo);
    }

    @Override
    public ApartmentInfo getDetailById(Long id) {
        ApartmentInfo apartmentInfo = this.getById(id);
        if (apartmentInfo == null) {
            return null;
        }

        List<GraphVo> graphVoList = graphInfoMapper.selectListByItemTypeAndId(ItemType.APARTMENT, id);

        List<LabelInfo> labelInfoList = labelInfoMapper.selectListByApartmentId(id);

        List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectListByApartmentId(id);

        List<FeeValueVo> feeValueVoList = feeValueMapper.selectListByApartmentId(id);

        ApartmentDetailVo adminApartmentDetailVo = new ApartmentDetailVo();
        BeanUtils.copyProperties(apartmentInfo, adminApartmentDetailVo);

        adminApartmentDetailVo.setGraphVoList(graphVoList);
        adminApartmentDetailVo.setLabelInfoList(labelInfoList);
        adminApartmentDetailVo.setFacilityInfoList(facilityInfoList);
        adminApartmentDetailVo.setFeeValueVoList(feeValueVoList);

        return adminApartmentDetailVo;
    }
}
